package model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import view.SonarView;

import battleship.ConfigureGame;
import battleship.Observer;
import battleship.Subject;

import exceptions.IncorrectPositionException;
import exceptions.MissException;

//grid e observer al lui ship
public class Grid implements Observer,Subject
{
	private int n = ConfigureGame.HIGHER_BOUNDARY - ConfigureGame.LOWER_BOUNDARY +1;
	private BlockInterface[][] table;
	private int ships_on_grid;
	private int activate_sonar = 0;
	private int nr_sonar_pulse = 2;
	private boolean use_space_lanser = false;
	private Observer mf;
	private SonarView sonar_view;
	
	public Grid()
	{
		this.table = new Block[n][n];
		this.ships_on_grid = 0;	
	}
	
	public void positionShip(ShipInterface ship) throws IncorrectPositionException
	{
		this.ships_on_grid++;
		ship.positionShip(this.table);
	}
	
	
	
	private void shootSonar()
	{
		BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
		String aw;
		System.out.println("Mai aveti "+this.nr_sonar_pulse+" Sonar Pulse.Lanseaza?(DA / NU)");
		try {
			aw = buff.readLine();
			if(aw.equals("DA") || aw.equals("da"))
			{
				this.nr_sonar_pulse--;
				this.sonar_view = new SonarView();
				//se afiseaza zona de pe grid conform pattern-ului cerut
				this.sonar_view.show(this.table,n);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void fire()
	{
		int cx, cy;
		
		BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
		try
		{
			if(this.activate_sonar == 1 && this.nr_sonar_pulse > 0)
			{
				this.shootSonar();
			}
			System.out.println("Se introduc coordonatele blocului atacat!");
			//do
			//{
				System.out.println("Coordonata Orizontala (cx):= ");
				cx = Integer.parseInt(buff.readLine());
				System.out.println("Coordonata Verticala (cy):= ");
				cy = Integer.parseInt(buff.readLine());
				
				if (cx <= ConfigureGame.HIGHER_BOUNDARY && cx >= ConfigureGame.LOWER_BOUNDARY &&
					cy <= ConfigureGame.HIGHER_BOUNDARY && cy >= ConfigureGame.LOWER_BOUNDARY)
				{
					System.out.println("Blocul ("+cx+","+cy+") a fost atacat!\nRezultat: ");
					if(table[cx-1][cy-1] == null)
					{
						System.out.println("MISSED!");
						table[cx-1][cy-1] = new Block(ConfigureGame.DEAD_BLOCK);
					}
					else
					{
						if(this.use_space_lanser == false)
							table[cx-1][cy-1].destroyBlock(new Bomb());
						else
							table[cx-1][cy-1].destroyBlock(new SpaceLancer());
					}
				}
			//}while()
		}catch(IOException e){
			
		} catch (MissException e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		}
		//return ConfigureGame.CONTINUE;
	}

	@Override
	public void update(Subject subject, Object data) 
	{
		//System.out.println(this.ships_on_grid);
		if(this.activate_sonar == 0)
			this.activate_sonar = 1;
		if(this.use_space_lanser == false)
			this.use_space_lanser = true;
		this.ships_on_grid--;
		if(this.ships_on_grid == 0)
			notifyObservers();
		
	}

	@Override
	public void addObserver(Observer observer) 
	{
		this.mf = observer;
	}

	@Override
	public void notifyObservers() 
	{
		((ModelFacade)mf).update(this, null);
	}

	@Override
	public void removeObserver(Observer observer) 
	{
		// TODO Auto-generated method stub
	}
	
	
}		
